package com.tyl.state;

public class StopTest implements Runnable {

    //1、设置一个标志位
    private  boolean flag=true;
    @Override
    public void run() {

        int i=0;
        while(flag){
            System.out.println("run....thread"+i++);
        }
    }

    //设置公开方法，停止线程用
    public void stop(){
        this.flag=false;
    }

    public static void main(String[] args) {
        StopTest stopTest=new StopTest();
        new Thread(stopTest).start();
        for (int i = 0; i < 1000; i++) {
            System.out.println("main"+i);
            if(i==900)
            {
                stopTest.stop();
                System.out.println("该线程停止了");
            }
        }
    }
}
